package com.jxs.android.sxyw;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.log.ILog;
import com.jxs.android.sxyw.weiget.PopMsg;
import com.jxs.android.sxyw.weiget.TopBar;
import com.jxs.android.util.StatusHelper;

public class StandardStatecraft extends AbstractActivity implements OnClickListener {
	private Context context;
	private TopBar topBar;
	private LinearLayout loading_ll, location_sumbit_ll;
	private TextView location_longitude_display, location_latitude_display, location_elevation_display;
	private Button location_sumbit;

	private double latitude = 0.0;
	private double longitude = 0.0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.context = this;
		initTopBar(savedInstanceState, R.layout.location_submit);
		loading_ll = (LinearLayout) findViewById(R.id.loading_ll);
		location_sumbit_ll = (LinearLayout) findViewById(R.id.location_sumbit_ll);
		location_longitude_display = (TextView) findViewById(R.id.location_longitude_display);
		location_latitude_display = (TextView) findViewById(R.id.location_latitude_display);
		location_elevation_display = (TextView) findViewById(R.id.location_elevation_display);

		location_sumbit = (Button) findViewById(R.id.location_sumbit);
		location_sumbit.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putString("latitude", latitude + "");
		bundle.putString("longitude", longitude + "");
		bundle.putSerializable("hasLocation", true);
		intent.putExtras(bundle);
		setResult(RESULT_OK, intent);
		this.finish();
	}

	@Override
	public void topBarLogic(TopBar topBar1) {
		this.topBar = topBar1;
		topBar.hindBtn();
		topBar.setTitle(R.string.location_submit_title_label);
		initTopBarReturn(topBar);
		topBar.setLogicBtnText("重新获取");
		topBar.setTopBarBtnListern(new TopBar.LogicBtnListern() {
			@Override
			public void onClickLogicBtn() {
				initLocation(context);
			}
		});
	}

	private LocationManager locationManager;
	private Timer overtimeTimer = new Timer(true);

	private Handler overtimeHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				LocationListener locationListener = (LocationListener) msg.obj;
				locationManager.removeUpdates(locationListener);
				sendLocation(null);
			}
		}
	};

	private void initLocation(Context mContext) {
		location_sumbit_ll.setVisibility(View.GONE);
		loading_ll.setVisibility(View.VISIBLE);
		if (locationManager == null)
			locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
		startGPSLocation(mContext);
	}

	private void startGPSLocation(final Context mContext) {
		LocationTask locationTask = new LocationTask();
		LocationListener gpslocationListener = new TaskLocationListener(locationTask);
		locationTask.setGpslocationListener(gpslocationListener);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, gpslocationListener);
		overtimeTimer.schedule(locationTask, 180000);
	}

	private class TaskLocationListener implements LocationListener {
		private TimerTask task;

		public TaskLocationListener(TimerTask task) {
			this.task = task;
		}

		@Override
		public void onLocationChanged(Location location) {
			ILog.e("StandardStatecraft", "fetch location");
			task.cancel();
			locationManager.removeUpdates(this);
			sendLocation(location);
		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	}

	private class LocationTask extends TimerTask {
		private LocationListener gpslocationListener;

		@Override
		public void run() {
			Message msg = new Message();
			msg.what = 1;
			msg.obj = gpslocationListener;
			overtimeHandler.sendMessage(msg);
		}

		public void setGpslocationListener(LocationListener gpslocationListener) {
			this.gpslocationListener = gpslocationListener;
		}
	}

	/**
	 * @Des 发送广播
	 * @param location
	 */
	private void sendLocation(Location location) {// 更新位置信息
		loading_ll.setVisibility(View.GONE);
		if (location != null) {
			latitude = location.getLatitude();
			longitude = location.getLongitude();
			location_sumbit_ll.setVisibility(View.VISIBLE);
			location_elevation_display.setText(location.getAccuracy() + "");
		} else {
			PopMsg.show("获取位置超时，请重新获取..");
			location_elevation_display.setText("");
			topBar.showBtn();
		}
		location_latitude_display.setText(latitude + "");
		location_longitude_display.setText(longitude + "");

	}

	@Override
	protected void onResume() {
		super.onResume();
		StatusHelper.checkGPS(context);
		if (StatusHelper.isGPSEnabled(context)) {
			initLocation(context);
		}
	}

}
